﻿using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;

namespace 任务栏和系统托盘图标
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private NotifyIcon notifyIcon;

        public MainWindow()
        {
            InitializeComponent();
            InitNotifyIcon();
        }

        public void InitNotifyIcon()
        {
            notifyIcon = new NotifyIcon()
            {
                Text = "任务栏图标",
                Visible = true,
            };
            // 1. 将图标设置为资源时的访问方式
            //System.IO.Stream stream = System.Windows.Application.GetResourceStream(new Uri("pack://application:,,,/Resources/Pikachu.ico")).Stream;
            // 2. 将图标设置为嵌入的资源时的访问方式
            string resourceName = "任务栏和系统托盘图标.Resources.Pikachu.ico";
            System.IO.Stream? stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
            notifyIcon.Icon = new Icon(stream);

            ContextMenuStrip contextMenu = new ContextMenuStrip();

            ToolStripMenuItem menuItem1 = new ToolStripMenuItem("设置");
            menuItem1.Click += MenuItem1_Click1;

            ToolStripMenuItem menuItem2 = new ToolStripMenuItem("关闭");
            menuItem2.Click += MenuItem1_Click1;
           
            contextMenu.Items.Add(menuItem1);
            contextMenu.Items.Add(menuItem2);
            notifyIcon.ContextMenuStrip = contextMenu;
        }

        private void MenuItem1_Click1(object? sender, EventArgs e)
        {
            System.Windows.Forms.MessageBox.Show("NotifyIcon Message");
        }
    }
}